﻿package mortal.game.view.heroArena.view
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.mui.utils.*;
    import flash.utils.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.heroArena.util.*;
    import mortal.mvc.core.*;

    public class HeroArenaFightDetailWin extends CopyInfoPanelBase
    {
        private var _leftTime:SecTimerBitmapView;
        private var _frameTimer:FrameTimer;
        private var _fightInfoArr:Array;
        private static var _instance:HeroArenaFightDetailWin;

        public function HeroArenaFightDetailWin()
        {
            this._fightInfoArr = new Array();
            super(ImagesConst.copyInfoPanelTitle_HeroArena, 262, 554);
            this.layer = LayerManager.uiLayer;
            this.mouseEnabled = false;
            if (_instance)
            {
                throw new Error("HeroArenaFightDetailWin 单例");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:HeroArenaFightInfoSpr = null;
            var _loc_2:HeroArenaFightInfoSpr = null;
            super.createDisposedChildrenImpl();
            _bodySprite.y = _bodySprite.y - 2;
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.HeroArenaFightBg, 0, 0, _bodySprite));
            _loc_1 = UICompomentPool.getUICompoment(HeroArenaFightInfoSpr);
            _loc_1.x = 55 + 2;
            _loc_1.y = 44 + 2;
            _bodySprite.addChild(_loc_1);
            this.pushUIToDisposeVec(_loc_1);
            this._fightInfoArr.push(_loc_1);
            _loc_2 = UICompomentPool.getUICompoment(HeroArenaFightInfoSpr);
            _loc_2.x = 55 + 2;
            _loc_2.y = 44 + 105 + 2;
            _bodySprite.addChild(_loc_2);
            this.pushUIToDisposeVec(_loc_2);
            this._fightInfoArr.push(_loc_2);
            var _loc_3:* = UICompomentPool.getUICompoment(HeroArenaFightInfoSpr);
            _loc_3.x = 55 + 2;
            _loc_3.y = 44 + 105 * 2 + 4;
            _bodySprite.addChild(_loc_3);
            this.pushUIToDisposeVec(_loc_3);
            this._fightInfoArr.push(_loc_3);
            var _loc_4:* = UICompomentPool.getUICompoment(HeroArenaFightInfoSpr);
            _loc_4.x = 55 + 2;
            _loc_4.y = 44 + 105 * 3 + 4;
            _bodySprite.addChild(_loc_4);
            this.pushUIToDisposeVec(_loc_4);
            this._fightInfoArr.push(_loc_4);
            this._leftTime = UIFactory.secTimerBitmapView(105 + 3, 483 + 12, "DoubleRewardTimerNum.png", 18.18, 27, 0, _bodySprite, 10);
            this._leftTime.customCharIndex = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, ::10};
            this._leftTime.setParse("mm:ss");
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.Scene_AddEntityUser, this.onSceneAddEntityHandler);
            Dispatcher.addEventListener(EventName.Scene_RemoveEntity, this.onSceneRemoveEntityHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.Scene_AddEntityUser, this.onSceneAddEntityHandler);
            Dispatcher.removeEventListener(EventName.Scene_RemoveEntity, this.onSceneRemoveEntityHandler);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show();
            this.initRoleInfo();
            return;
        }// end function

        public function setLeftTime(param1:int) : void
        {
            if (this._leftTime)
            {
                this._leftTime.setLeftTime(param1);
            }
            return;
        }// end function

        private function initRoleInfo() : void
        {
            if (Game.isSceneInit() && RolePlayer.instance.entityInfo)
            {
                this.initInfo();
            }
            else
            {
                if (!this._frameTimer)
                {
                    this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                    this._frameTimer.addListener(TimerType.ENTERFRAME, this.onSceneInit);
                }
                this._frameTimer.start();
            }
            return;
        }// end function

        private function onSceneInit(param1:FrameTimer) : void
        {
            if (Game.isSceneInit() && RolePlayer.instance.entityInfo)
            {
                if (this._frameTimer)
                {
                    this._frameTimer.stop();
                }
                this.initInfo();
            }
            return;
        }// end function

        private function initInfo() : void
        {
            var _loc_2:IEntity = null;
            var _loc_1:* = ThingUtil.entityUtil.entitysMap.allEntitys;
            for each (_loc_2 in _loc_1)
            {
                
                if (_loc_2 is UserPlayer && _loc_2.entityInfo)
                {
                    this.setEntityInfo(_loc_2.entityInfo);
                }
            }
            return;
        }// end function

        private function setEntityInfo(param1:EntityInfo) : void
        {
            if (HeroArenaUtil.isObserverModel && EntityUtil.isSelf(param1.entityInfo.entityId))
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._fightInfoArr.length)
            {
                
                if (this._fightInfoArr[_loc_2].playerInfo != null)
                {
                }
                else if (!this.isInMyTeam(param1) && _loc_2 < 2)
                {
                }
                else
                {
                    this._fightInfoArr[_loc_2].data = param1;
                    return;
                }
                _loc_2++;
            }
            return;
        }// end function

        private function isInMyTeam(param1:EntityInfo) : Boolean
        {
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_2:* = Cache.instance.heroArena.myTeamInfo;
            if (_loc_2 && _loc_2.captainPlayer && _loc_2.memberPlayer)
            {
                _loc_3 = _loc_2.captainPlayer.name;
                _loc_4 = _loc_2.memberPlayer.name;
                _loc_5 = param1.entityInfo.name;
                if (_loc_5 == _loc_3 || _loc_5 == _loc_4)
                {
                    return true;
                }
            }
            return false;
        }// end function

        private function onSceneAddEntityHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as UserPlayer;
            if (_loc_2 && _loc_2.entityInfo)
            {
                if (this.isPlayerClone(_loc_2.entityInfo))
                {
                    return;
                }
                this.updateOneFightInfo(_loc_2.entityInfo);
            }
            return;
        }// end function

        private function onSceneRemoveEntityHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (_loc_2)
            {
                this.clearOneFightInfo(_loc_2);
            }
            return;
        }// end function

        private function updateOneFightInfo(param1:EntityInfo) : void
        {
            var _loc_3:HeroArenaFightInfoSpr = null;
            var _loc_2:* = this.getFighInfoByRoleName(param1.entityInfo.name);
            if (_loc_2)
            {
                _loc_2.data = param1;
            }
            else
            {
                for each (_loc_3 in this._fightInfoArr)
                {
                    
                    if (_loc_3.playerInfo == null)
                    {
                        _loc_3.data = param1;
                        return;
                    }
                }
            }
            return;
        }// end function

        private function hasSameEntity(param1:EntityInfo) : Boolean
        {
            var _loc_2:HeroArenaFightInfoSpr = null;
            for each (_loc_2 in this._fightInfoArr)
            {
                
                if (_loc_2.roleName && _loc_2.roleName == param1.entityInfo.name)
                {
                    return true;
                }
            }
            return false;
        }// end function

        private function clearOneFightInfo(param1:SEntityId) : void
        {
            var _loc_2:* = this.getFightInfoByEntityId(param1);
            if (_loc_2)
            {
                _loc_2.clear();
            }
            return;
        }// end function

        private function getFighInfoByRoleName(param1:String) : HeroArenaFightInfoSpr
        {
            var _loc_2:HeroArenaFightInfoSpr = null;
            for each (_loc_2 in this._fightInfoArr)
            {
                
                if (_loc_2.roleName && _loc_2.roleName == param1)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        private function getFightInfoByEntityId(param1:SEntityId) : HeroArenaFightInfoSpr
        {
            var _loc_2:HeroArenaFightInfoSpr = null;
            for each (_loc_2 in this._fightInfoArr)
            {
                
                if (_loc_2.entityId && EntityUtil.equal(_loc_2.entityId, param1))
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        private function isPlayerClone(param1:EntityInfo) : Boolean
        {
            if (param1.entityInfo.ownerEntityId && param1.entityInfo.ownerEntityId.id != 0)
            {
                return true;
            }
            return false;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._leftTime.dispose();
            this._leftTime = null;
            if (this._frameTimer && this._frameTimer.running)
            {
                this._frameTimer.stop();
            }
            this._frameTimer = null;
            this._fightInfoArr.length = 0;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : HeroArenaFightDetailWin
        {
            if (_instance == null)
            {
                _instance = new HeroArenaFightDetailWin;
            }
            return _instance;
        }// end function

        public static function isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
